package github.leavesczy.reactivehttpsamples.widget.view

import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import com.blankj.utilcode.util.ActivityUtils
import github.leavesczy.reactivehttpsamples.databinding.HeaderLayoutBinding

/**
 * 针对导航栏的统一封装使用
 */
class HeaderView : FrameLayout {


    private var mClickListener: HeaderClickListener? = null

    private var mRightTextClick: Boolean? = true

    private lateinit var binding: HeaderLayoutBinding

    constructor(context: Context) : super(context) {
        initView(context)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initView(context)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        initView(context)
    }

    private fun initView(context: Context) {
        binding = HeaderLayoutBinding.inflate(LayoutInflater.from(context), null, false)
        addView(binding.root)
    }


    //==============左侧返回相关设置========================
    //是否启用默认返回样式
    fun enableBack(back: Boolean): HeaderView {
        if (back) {
            setVisible(binding.llBack)

        } else {
            setInVisible(binding.llBack)
        }
        return this
    }

    //设置其他返回样式
    fun setBackIcon(@DrawableRes backResId: Int): HeaderView {
        if (backResId != 0) {
            binding.ivBack.setImageResource(backResId)
            enableBack(true)
        } else {
            enableBack(false)
        }
        return this
    }

    //=================标题相关设置============================
    //标题相关设置
    fun setTitle(title: String): HeaderView {
        binding.tvTitle.text = title
        return this
    }

    fun setTitleColor(@ColorInt color: Int): HeaderView {
        binding.tvTitle.setTextColor(color)
        return this
    }

    fun setTitleVisible(visibilty: Boolean): HeaderView {
        if (visibilty) setVisible(binding.tvTitle) else setGone(binding.tvTitle)
        return this
    }

    //==============右侧文字和图片相关设置===============================


    fun setClickListener(clickListener: HeaderClickListener): HeaderView {
        mClickListener = clickListener
        return this
    }


    interface HeaderClickListener {

        fun onBackClick(view: View)

    }

    private fun setVisible(view: View) {
        if (view.visibility != View.VISIBLE) {
            view.visibility = View.VISIBLE
        }
    }

    private fun setInVisible(view: View) {
        if (view.visibility != View.INVISIBLE) {
            view.visibility = View.INVISIBLE
        }
    }

    private fun setGone(view: View) {
        if (view.visibility != View.GONE) {
            view.visibility = View.GONE
        }
    }

    fun init(act:Activity){
        binding.llBack.setOnClickListener {
            ActivityUtils.finishActivity(act)
        }
    }
    fun initBack(l:View.OnClickListener?){
        binding.llBack.setOnClickListener(l)
    }

    fun initRightText(rightName:String,l:View.OnClickListener?){
        setVisible(binding.flRightText)
        binding.tvRightText.text=rightName
        binding.flRightText.setOnClickListener(l)
    }
}
